package org.vectrics.client.framework;


import javax.swing.JButton;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import org.apache.log4j.*;
import org.vectrics.VectricsException;
import org.vectrics.imaging.client.ImagePanel;
import org.vectrics.swing.GuiManager;



public class ErrorFrame extends JDialog {
    private transient static Logger log = Logger.getLogger(ErrorFrame.class);
    private JTextArea errorMessageText = new JTextArea();
	private JTextArea detailMessageText = new JTextArea();
	private static JFrame ownerFrame = null;

	public static void intitialize(JFrame owner) {
		ownerFrame = owner;
	}

	public static void showError(String title, String error, String details) {
		try {
			if (ownerFrame == null) {
				throw new VectricsException("ErrorFrame.showError(title, error, details) should only be called after the initialize(frame) method is called.");
			}
			showError(ownerFrame, title, error, details);
		}
		catch (Exception ex) {
			log.error("Exception showing error", ex);
		}
	}
	
	public static void showError(JFrame owner, String title, String error, String details) {
		ErrorFrame frame = new ErrorFrame(owner, title); 
		frame.setLocationRelativeTo(owner);

		log.debug("centering the window");
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
        

		log.debug("making main form visible");
		//frame.setVisible(true);
		frame.setMessage(title, error, details);
		frame.pack();		
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		int x = (screenSize.width - frameSize.width) / 2;
		int y = (screenSize.height - frameSize.height) / 2;
		//frame.setLocation(x, y);
		frame.setBounds(x, y, 400, 250);
		frame.show();
		frame.setResizable(false);
	}


    
    /**
    * Constructor 
    */
      private ErrorFrame(Frame owner, String title) {
      	super(owner, title, false);
        log.debug("ClientFrame constructor called");
        
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
            log.info("creating default server connection");
            
            
            log.info("initializing GUI");
            initComponents();
            this.pack();
			log.info("done intializing GUI - setting up connections");
        }
        catch(Exception e) {
            log.error("ClientFrame constructor encountered an exception", e);
        }
  }


    
  /**
   * Component initialization
   */
  private void initComponents() throws Exception  {
  		log.debug("initComponents() called");
        JPanel mainPanel = new JPanel();
        
        this.getContentPane().add(mainPanel);
        mainPanel.setLayout(new BorderLayout());
        
        //JPanel leftPanel = new JPanel();
        //leftPanel.setBorder(GuiManager.getInstance().getEtchedBorder());
        //leftPanel.setBackground(Color.CYAN);
        //leftPanel.setLayout(new FlowLayout());
        ImagePanel errorImagePanel = new ImagePanel();
        errorImagePanel.setScale(100);
        errorImagePanel.showFile("error.gif");
        errorImagePanel.setBorder(GuiManager.getInstance().getEtchedBorder());
        errorImagePanel.setBackground(Color.BLACK);
        errorImagePanel.setPreferredSize(new Dimension(errorImagePanel.getImageWidth(), 
        			errorImagePanel.getImageHeight()));
        //leftPanel.add(errorImagePanel);
        errorImagePanel.setBorder(GuiManager.getInstance().getSunkenBorder());
        
        mainPanel.add(errorImagePanel, BorderLayout.WEST);
        JPanel rightPanel = new JPanel();
        rightPanel.setBorder(GuiManager.getInstance().getEtchedBorder());
        mainPanel.add(rightPanel, BorderLayout.CENTER);
        
        JPanel messagePanel = new JPanel();
        messagePanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BorderLayout());
        rightPanel.add(messagePanel, BorderLayout.CENTER);
        JPanel errorMessagePanel = new JPanel();
        JPanel detailMessagePanel = new JPanel();
        
        
        JPanel buttonPanel = new JPanel();
        rightPanel.add(buttonPanel, BorderLayout.SOUTH);
        
/*        
        titleMessageLabel.setMaximumSize(new Dimension(200, 20));
        titleMessageLabel.setBackground(GuiManager.getBackgroundColor());
        titleMessageLabel.setBorder(new EtchedBorder());
        titleMessageLabel.setEditable(false);
  */
		errorMessageText.setBackground(GuiManager.getInstance().getBackgroundColor());
		errorMessageText.setBorder(new BevelBorder(BevelBorder.LOWERED));
		errorMessageText.setBackground(this.getBackground());
		errorMessageText.setLineWrap(true);
		errorMessageText.setEditable(false);
		errorMessageText.setFont(GuiManager.getInstance().getStandardFont());
		errorMessageText.setWrapStyleWord(true);
		
		detailMessageText.setBackground(GuiManager.getInstance().getBackgroundColor());
		detailMessageText.setBackground(this.getBackground());
		detailMessageText.setBorder(new BevelBorder(BevelBorder.LOWERED));
		detailMessageText.setEditable(false);
		detailMessageText.setFont(GuiManager.getInstance().getStandardFont());
		detailMessageText.setLineWrap(true);
		detailMessageText.setWrapStyleWord(true);
		
		JLabel errorLabel = new JLabel();
		errorLabel.setFont(GuiManager.getInstance().getStandardFont());
		errorLabel.setText("Error Message");
		
		JLabel detailLabel = new JLabel();
		detailLabel.setFont(GuiManager.getInstance().getStandardFont());
		detailLabel.setText("Details");
		
		errorMessagePanel.setLayout(new BorderLayout());
		Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 3, 3);
		errorMessagePanel.setBorder(emptyBorder);
		
		errorMessagePanel.add(errorLabel, BorderLayout.NORTH);
		errorMessagePanel.add(errorMessageText, BorderLayout.CENTER);
		
		detailMessagePanel.setLayout(new BorderLayout());
		detailMessagePanel.setBorder(emptyBorder);
		detailMessagePanel.add(detailLabel, BorderLayout.NORTH);
		detailMessagePanel.add(detailMessageText, BorderLayout.CENTER);
		
		messagePanel.add(errorMessagePanel, BorderLayout.NORTH);
		messagePanel.add(detailMessagePanel, BorderLayout.CENTER);
		JButton okButton = GuiManager.getInstance().createButton("Ok");
		buttonPanel.add(okButton);
		okButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				okButtonPressed(e);
			}
		});
		
    }
    
    
    
    public void setMessage(String caption, String message, String details) {
    	this.errorMessageText.setText(message);
    	this.detailMessageText.setText(details);
    	
    	if ((details == null) || (details.length() <= 0))
    		detailMessageText.setVisible(false);	
    }

	protected void okButtonPressed(ActionEvent e) {
		this.dispose();
	}

}